package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV2;

import com.education.realtime.bean.UserAppBean;
import com.education.realtime.common.Constant;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Map;

public class DwsUserUesrRegisterWindow extends BaseAppV2 {
    public static void main(String[] args) {
        new DwsUserUesrRegisterWindow().init(3004,"DwsUserUesrRegisterWindow", Constant.TOPIC_DWD_USER_REGISTER,Constant.TOPIC_DWD_TRAFFIC_UV_DETAIL);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, Map<String, DataStreamSource<String>> stream) {
        DataStream<UserAppBean> unionStream = union(stream);

        //开窗聚合
        SingleOutputStreamOperator<UserAppBean> countBean = windowCOunt(unionStream);
//        countBean.print();

        countBean.addSink(FlinkSinkUtil.getClickHouseSink(Constant.CLICKHOUSE_DB,
                "dws_user_user_regiter_window", UserAppBean.class));
    }

    private SingleOutputStreamOperator<UserAppBean> windowCOunt(DataStream<UserAppBean> unionStream) {
        return unionStream
                .assignTimestampsAndWatermarks(WatermarkStrategy.<UserAppBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((obj, ts) -> obj.getTs()))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<UserAppBean>() {
                    @Override
                    public UserAppBean reduce(UserAppBean value1, UserAppBean value2) throws Exception {
                        value1.setUrCt(value1.getUrCt() + value2.getUrCt());
                        value1.setUvCt(value1.getUvCt() + value2.getUvCt());
                        return value1;
                    }
                }, new ProcessAllWindowFunction<UserAppBean, UserAppBean, TimeWindow>() {
                    @Override
                    public void process(Context ctx,
                                        Iterable<UserAppBean> elements,
                                        Collector<UserAppBean> out) throws Exception {
                        String stt = AtguiguUtil.toDate(ctx.window().getStart(), "yyyy-MM-dd HH:mm:ss");
                        String edt = AtguiguUtil.toDate(ctx.window().getEnd(), "yyyy-MM-dd HH:mm:ss");
                        UserAppBean next = elements.iterator().next();
                        next.setEdt(edt);
                        next.setStt(stt);
                        out.collect(next);
                    }
                });
    }

    private DataStream<UserAppBean> union(Map<String, DataStreamSource<String>> stream) {
        SingleOutputStreamOperator<UserAppBean> uvStream = stream.get(Constant.TOPIC_DWD_USER_REGISTER)
                .map(new MapFunction<String, UserAppBean>() {
                    @Override
                    public UserAppBean map(String value) throws Exception {
                        JSONObject obj = JSON.parseObject(value);
                        return new UserAppBean( "","",1L,0L,obj.getLong("ts")*1000);
                    }
                });

        SingleOutputStreamOperator<UserAppBean> pageStream = stream.get(Constant.TOPIC_DWD_TRAFFIC_UV_DETAIL)
                .map(new MapFunction<String, UserAppBean>() {
                    @Override
                    public UserAppBean map(String value) throws Exception {
                        JSONObject obj = JSON.parseObject(value);
                        return new UserAppBean("", "", 0L, 1L, obj.getLong("ts"));
                    }
                });
        return pageStream.union(uvStream);
    }
}
